package com.dragon.logical.service.supplies.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.logical.service.supplies.IStockService;
import com.dragon.repository.entity.ProductCategory;
import com.dragon.repository.entity.Stock;
import com.dragon.repository.mapper.ProductCategoryMapper;
import com.dragon.repository.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-11-13
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {


    @Autowired
    StockMapper stockMapper;
    @Autowired
    ProductCategoryMapper categoryMapper;

    @Override
    public Page<Stock> stockList(Integer page, Integer limit, String search, Integer warehouseId, Integer categoryId) {
        Page<Stock> stockPage = new Page<>(page, limit);
        Page<Stock> stocks = null;
        QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ProductCategory::getPid, categoryId);
        List<ProductCategory> categoryList = categoryMapper.selectList(wrapper);
        if (categoryList.size() == 0){
            stocks = stockMapper.selectByName(stockPage, search, warehouseId, categoryId);
        } else {
            List<Integer> ids = categoryList.stream().map(ProductCategory::getId).collect(Collectors.toList());
            stocks = stockMapper.selectByNameTwo(stockPage,search,warehouseId,ids);
        }
        return stocks;
    }

}
